package com.bootdo.wx.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import com.alipay.api.domain.GoodsDetail;
import com.bootdo.common.utils.PageHelperUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.wx.dao.*;
import com.bootdo.wx.domain.*;
import com.bootdo.wx.service.GoodsGoodsService;
import com.bootdo.wx.service.MemberCartService;
import com.bootdo.wx.service.OrderDetailService;
import com.bootdo.wx.service.OrderTeamMoneyService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;


@Service
public class OrderTeamMoneyServiceImpl implements OrderTeamMoneyService {
	@Autowired
	private OrderTeamMoneyDao orderTeamMoneyDao;

	@Autowired
	private CmsContentDao cmsContentDao;

	@Autowired
	private OrderOrderDao orderOrderDao;
	@Autowired
	private MemberUserDao memberUserDao;
	@Autowired
	private OrderDetailDao orderDetailDao;
	@Autowired
	private GoodsGoodsService goodsGoodsService;
	@Override
	public OrderTeamMoneyDO get(String id){
		return orderTeamMoneyDao.get(id);
	}
	
	@Override
	public List<OrderTeamMoneyDO> list(Map<String, Object> map){
		return orderTeamMoneyDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return orderTeamMoneyDao.count(map);
	}
	
	@Override
	public int save(OrderTeamMoneyDO orderTeamMoney){
		return orderTeamMoneyDao.save(orderTeamMoney);
	}
	
	@Override
	public int update(OrderTeamMoneyDO orderTeamMoney){
		return orderTeamMoneyDao.update(orderTeamMoney);
	}
	
	@Override
	public int remove(String id){
		return orderTeamMoneyDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return orderTeamMoneyDao.batchRemove(ids);
	}

    @Override
    public List<OrderTeamMoneyDO> likeList(Map<String, Object> map){
        return orderTeamMoneyDao.likeList(map);
    }

    @Override
    public R getUpgradeRules(Map<String, Object> params) {
		String userId = MapUtil.getStr(params, "userId");
		Map<String,Object> dateMap = new HashMap<>();
		Date strDate ;
		Date endDate ;
		int i = DateUtil.dayOfMonth(new Date());
		if(i==1){
			//上月2号开始时间
			strDate= DateUtil.beginOfDay(DateUtil.offsetDay(DateUtil.beginOfMonth(DateUtil.lastMonth()),1));
			endDate=DateUtil.endOfDay(new Date());
		}else{
			//这个月2号为开始时间
			strDate= DateUtil.beginOfDay(DateUtil.offsetDay(DateUtil.beginOfMonth(new Date()),1));
			endDate=DateUtil.endOfDay(new Date());
		}
		dateMap.put("strDate",strDate);
		dateMap.put("endDate",endDate);
		dateMap.put("userId",userId);
		if(MapUtil.getInt(params,"status")==0){
			//升级团长
			//查询升级所需订单
			String value = cmsContentDao.getByType("num-tz").getValue();
			//查询订单数,已收货的订单数
			int count = orderOrderDao.countOrderNumByUserIdAndLower(userId);
			Map<String,Object> map = new HashMap<>();
			map.put("num",Integer.parseInt(value));
			map.put("orderCount",count);
			return R.ok().put("data",map);
		}else if(MapUtil.getInt(params,"status")==1){
        	//升级核心
			//保级所需业绩
			String downAllMoney = cmsContentDao.getByType("money-tz-down").getValue();
			//已完成业绩

			BigDecimal overMoney =  orderTeamMoneyDao.sumMoneyByUserId(dateMap);
			//百分比
			BigDecimal downRatio =overMoney .divide(new BigDecimal(downAllMoney), 2, BigDecimal.ROUND_HALF_UP);
			//晋级进度
			//晋级所需人数
			String upUserNum = cmsContentDao.getByType("num-tz-zt-up").getValue();
			//已直推人数
			Map<String,Object> map = new HashMap<>();
			map.put("introUser",userId);
			map.put("delFlag","0");
			int count = memberUserDao.count(map);
			//人数百分比
			BigDecimal upUserRation = new BigDecimal(count).divide(new BigDecimal(upUserNum), 2, BigDecimal.ROUND_HALF_UP);
			//升级所需业绩
			String upAllMoney = cmsContentDao.getByType("money-tz-up").getValue();
			//升级完成百分比
			BigDecimal upMoneyRation = overMoney.divide(new BigDecimal(upAllMoney), 2, BigDecimal.ROUND_HALF_UP);

			Map<String,Object> returnMap = new HashMap<>();
			returnMap.put("downAllMoney",Integer.parseInt(downAllMoney));
			returnMap.put("overMoney",overMoney);
			returnMap.put("downRatio",downRatio);
			returnMap.put("upUserNum",Integer.parseInt(upUserNum));
			returnMap.put("userNum",count);
			returnMap.put("upUserRation",upUserRation);
			returnMap.put("upAllMoney",Integer.parseInt(upAllMoney));
			returnMap.put("upMoneyRation",upMoneyRation);
			return R.ok().put("data",returnMap);



		}else if(MapUtil.getInt(params,"status")==2){
        	//升级合伙人

			//升级合伙人
			//保级所需业绩
			String downAllMoney = cmsContentDao.getByType("money-hx-down").getValue();
			//已完成业绩
			BigDecimal overMoney =  orderTeamMoneyDao.sumMoneyByUserId(dateMap);
			//百分比
			BigDecimal downRatio = overMoney .divide(new BigDecimal(downAllMoney), 2, BigDecimal.ROUND_HALF_UP);
			//晋级进度
			//晋级所需人数
			String upUserNum = cmsContentDao.getByType("num-hx-up").getValue();
			//已直推人数
			Map<String,Object> map = new HashMap<>();
			map.put("introUser",userId);
			map.put("delFlag","0");
			int count = memberUserDao.count(map);
			//人数百分比
			BigDecimal upUserRation =new BigDecimal(count).divide(new BigDecimal(upUserNum), 2, BigDecimal.ROUND_HALF_UP);
			//升级所需业绩
			String upAllMoney = cmsContentDao.getByType("money-hx-up").getValue();
			//升级完成百分比
			BigDecimal upMoneyRation = overMoney.divide(new BigDecimal(upAllMoney), 2, BigDecimal.ROUND_HALF_UP);

			Map<String,Object> returnMap = new HashMap<>();
			returnMap.put("downAllMoney",Integer.parseInt(downAllMoney));
			returnMap.put("overMoney",overMoney);
			returnMap.put("downRatio",downRatio);
			returnMap.put("upUserNum",Integer.parseInt(upUserNum));
			returnMap.put("userNum",count);
			returnMap.put("upUserRation",upUserRation);
			returnMap.put("upAllMoney",Integer.parseInt(upAllMoney));
			returnMap.put("upMoneyRation",upMoneyRation);
			return R.ok().put("data",returnMap);
		}else if(MapUtil.getInt(params,"status")==3){
        	//合伙人保级
			String downAllMoney = cmsContentDao.getByType("money-hhr-down").getValue();
			//已完成业绩
			BigDecimal overMoney =  orderTeamMoneyDao.sumMoneyByUserId(dateMap);
			//百分比
			BigDecimal downRatio =overMoney .divide(new BigDecimal(downAllMoney), 2, BigDecimal.ROUND_HALF_UP);
			Map<String,Object> returnMap = new HashMap<>();
			returnMap.put("downAllMoney",Integer.parseInt(downAllMoney));
			returnMap.put("overMoney",overMoney);
			returnMap.put("downRatio",downRatio);
			return R.ok().put("data",returnMap);
		}else{
        	return R.error("参数status有误");
		}


    }

	/**
	 * 根据团长id查询业绩详情
	 * @param query
	 * @return
	 */
	@Override
	public List<Map<String,Object>> listByApplyId(Query query) {
		if("".equals(query.get("strDate")) || "".equals(query.get("endDate"))){
			Date strDate ;
			Date endDate ;
			int i = DateUtil.dayOfMonth(new Date());
			if(i==1){
				//上月2号开始时间
				strDate= DateUtil.beginOfDay(DateUtil.offsetDay(DateUtil.beginOfMonth(DateUtil.lastMonth()),1));
				endDate=DateUtil.endOfDay(new Date());
			}else{
				//这个月2号为开始时间
				strDate= DateUtil.beginOfDay(DateUtil.offsetDay(DateUtil.beginOfMonth(new Date()),1));
				endDate=DateUtil.endOfDay(new Date());
			}
			query.put("strDate",strDate);
			query.put("endDate",endDate);
		}

		List<Map<String,Object>> list = orderTeamMoneyDao.listByApplyId(query);
		return list;
	}

	/**
	 * 根据团长id查询业绩详情列表的条数
	 * @param query
	 * @return
	 */
	@Override
	public int listByApplyIdTotal(Query query) {
		if("".equals(query.get("strDate")) || "".equals(query.get("endDate"))){
			Date strDate ;
			Date endDate ;
			int i = DateUtil.dayOfMonth(new Date());
			if(i==1){
				//上月2号开始时间
				strDate= DateUtil.beginOfDay(DateUtil.offsetDay(DateUtil.beginOfMonth(DateUtil.lastMonth()),1));
				endDate=DateUtil.endOfDay(new Date());
			}else{
				//这个月2号为开始时间
				strDate= DateUtil.beginOfDay(DateUtil.offsetDay(DateUtil.beginOfMonth(new Date()),1));
				endDate=DateUtil.endOfDay(new Date());
			}
			query.put("strDate",strDate);
			query.put("endDate",endDate);
		}
		int total = orderTeamMoneyDao.listByApplyIdTotal(query);
		return total;
	}

	/**
	 * 根据代理人id查询业绩详情
	 * @param query
	 * @return
	 */
	@Override
	public List<Map<String, Object>> listByUserId(Query query) {
		if("".equals(query.get("strDate")) || "".equals(query.get("endDate"))){
			Date strDate ;
			Date endDate ;
			int i = DateUtil.dayOfMonth(new Date());
			if(i==1){
				//上月2号开始时间
				strDate= DateUtil.beginOfDay(DateUtil.offsetDay(DateUtil.beginOfMonth(DateUtil.lastMonth()),1));
				endDate=DateUtil.endOfDay(new Date());
			}else{
				//这个月2号为开始时间
				strDate= DateUtil.beginOfDay(DateUtil.offsetDay(DateUtil.beginOfMonth(new Date()),1));
				endDate=DateUtil.endOfDay(new Date());
			}
			query.put("strDate",strDate);
			query.put("endDate",endDate);
		}

		List<Map<String,Object>> list = orderTeamMoneyDao.listByUserId(query);
		return list;
	}

	/**
	 * 根据代理人id查询业绩详情列表条数
	 * @param query
	 * @return
	 */
	@Override
	public int listByUserIdTotal(Query query) {
		if("".equals(query.get("strDate")) || "".equals(query.get("endDate"))){
			Date strDate ;
			Date endDate ;
			int i = DateUtil.dayOfMonth(new Date());
			if(i==1){
				//上月2号开始时间
				strDate= DateUtil.beginOfDay(DateUtil.offsetDay(DateUtil.beginOfMonth(DateUtil.lastMonth()),1));
				endDate=DateUtil.endOfDay(new Date());
			}else{
				//这个月2号为开始时间
				strDate= DateUtil.beginOfDay(DateUtil.offsetDay(DateUtil.beginOfMonth(new Date()),1));
				endDate=DateUtil.endOfDay(new Date());
			}
			query.put("strDate",strDate);
			query.put("endDate",endDate);
		}
		int total = orderTeamMoneyDao.listByUserIdTotal(query);
		return total;

	}

}
